﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class admin_addcustomer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Calendar1.Visible = true;
        
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        txtBrithday.Text = Calendar1.SelectedDate.ToString("dd/MM/yyyy");
        txtBrithday.Focus();
        Calendar1.Visible = false;
    }

    protected void txtUser_TextChanged(object sender, EventArgs e)
    {
        txtPass1.Focus();
        if (txtUser.Text == "")
        {
            lbUser.Text = "not null";
        }
        else
        {
            lbUser.Text = "ok";
        }
    }
    protected void txtPass1_TextChanged(object sender, EventArgs e)
    {

        txtPass2.Text = "";
        txtPass2.Focus();
        lbpass2.Text = "";
       
        
            if (txtPass1.Text == "")
            {
                lbPass1.Text = "not null";
                txtPass2.Text = "";
                lbpass2.Text = "";
            }
            else
            {
                lbPass1.Text = "ok";
       
            }
        
       
    }
    protected void txtPass2_TextChanged(object sender, EventArgs e)
    {
        txtFullname.Focus();
        if(txtPass2.Text=="")
        {
            lbpass2.Text = "not null";
            
        }
        else
        {
            if (txtPass2.Text == txtPass1.Text)
            {
                lbpass2.Text = "ok";
            }
            else
            {
                lbpass2.Text = "no";
            }
        }
    }
    protected void txtFullname_TextChanged(object sender, EventArgs e)
    {
        txtEmail.Focus();
        if (txtFullname.Text == "")
        {
            lbFullname.Text = "not null";
        }
        else
        {
            lbFullname.Text = "ok";
        }
    }
    protected void txtEmail_TextChanged(object sender, EventArgs e)
    {
        rdlGender.Focus();
        if (txtEmail.Text == "")
        {
            LbEmail.Text = "not null";
        }
        else
        {
            LbEmail.Text = "ok";
        }
    }
    protected void rdlGender_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (rdlGender.SelectedItem.Selected==true)
        {
            lbGender.Text = "ok";
        }
        else
        {
            lbGender.Text = "not null";
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (lbUser.Text == "ok" && lbPass1.Text == "ok" && lbpass2.Text == "ok" && lbFullname.Text == "ok" && LbEmail.Text == "ok" && lbGender.Text == "ok")
        {
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ShoppingOnlineConnectionString"].ConnectionString);
            string str = "insert into  [user] (User_ID,Username,Password,Fullname,Email,Gender,Birthdays,PhoneNumber,GrantID,Address)values(@id,@user,@pass,@full,@email,@gen,@birth,@phone,@gran,@address)";
            SqlCommand cmd = new SqlCommand(str, conn);
            int a = 1;
            a++;
            if (DropDownList1.SelectedItem.Value == "2")
            {
                cmd.Parameters.Add("@id", "EM" + a);
            }
            else
            {
                cmd.Parameters.Add("@id", "CU" + a);
            }
            
            cmd.Parameters.Add("@user", txtUser.Text);
            cmd.Parameters.Add("@pass", txtPass2.Text);
            cmd.Parameters.Add("@full", txtFullname.Text);
            cmd.Parameters.Add("@email", txtEmail.Text);

            

            if (rdlGender.SelectedItem.Value == "Male")
            {
                cmd.Parameters.Add("@gen", "True");
            }
            else
            {
                cmd.Parameters.Add("@gen", "False");
            }
            cmd.Parameters.Add("@birth",Convert.ToDateTime(txtBrithday.Text));
            cmd.Parameters.Add("@phone", txtPhone.Text);
            cmd.Parameters.Add("@gran", DropDownList1.SelectedItem.Value);
            cmd.Parameters.Add("@address", txtAddress.Text);



            conn.Open();

            cmd.ExecuteNonQuery();

            GridView1.EditIndex = -1;
            GridView1.DataBind();
            GridView1.Visible = true;
            Response.Write("chuc mung ban da add thanh cong");
            txtUser.Text = "";
            txtPass1.Text = "";
            txtPass2.Text = "";
            txtFullname.Text = "";
            txtEmail.Text = "";

            txtBrithday.Text = "";
            txtPhone.Text = "";
            lbUser.Text = "";
            lbPass1.Text = "";
            lbpass2.Text = "";
            lbFullname.Text = "";
            LbEmail.Text = "";
            lbGender.Text = "";
        }
        else
        {
            Response.Write("add that bai");
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtUser.Text = "";
        txtPass1.Text = "";
        txtPass2.Text = "";
        txtFullname.Text = "";
        txtEmail.Text = "";
        
        txtBrithday.Text = "";
        txtPhone.Text = "";
        lbUser.Text = "";
        lbPass1.Text = "";
        lbpass2.Text = "";
        lbFullname.Text = "";
        LbEmail.Text = "";
        lbGender.Text = "";

        
    }
}
